<template>
  <div class="content-wrapper">
    <div class="nav">
      <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/administration-performanceList' }">奖惩动态</el-breadcrumb-item>
        <el-breadcrumb-item>绩效列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="content">
      <div class="search-bar">
        <div></div>
        <el-date-picker type="month" placeholder="选择月份" v-model="searchData.kpiDate" style="width: 180px;" value-format="yyyy-MM" :clearable="false"></el-date-picker>
      </div>
      <div class="main-content">
        <el-table :data="departmentList" style="width: 100%;"
          row-key="id"
          border
          lazy
          :load="getKpiList"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :span-method="arraySpanMethod">
          <el-table-column prop="name" label="部门" min-width="100">
            <template slot-scope="scope">
              <span><span v-if="scope.row.children && scope.row.children.length === 0" style="width: 24px; display: inline-block;"></span>{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" min-width="100" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="getKpiListByUser(scope.row)" style="padding: 0;">{{scope.row.dingName}}</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="kpiLevel" label="绩效等级" min-width="100" align="center"></el-table-column>
          <el-table-column prop="kpiRatio" label="绩效系数" min-width="100" align="center"></el-table-column>
          <el-table-column prop="kpiScores" label="绩效得分" min-width="100" align="center"></el-table-column>
          <el-table-column label="操作" min-width="180" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="showDetailDialog(scope.row)" style="padding: 0;">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog
      class="dialog-style"
      :visible.sync="userKpiDialogVisible"
      :fullscreen="true"
      :show-close="false">
      <div slot="title" class="title">
        <el-button class="back" type="text" @click="userKpiDialogVisible=false;">返回</el-button>
        <span>个人绩效列表</span>
      </div>
      <div class="content">
        <div class="info">
          <div class="info-item">{{curUser.dingName}}</div>
          <div class="info-item" v-if="curUser.department">【{{curUser.department}}】</div>
        </div>
        <div>
          <el-table :data="userKpiList" :height="tableHeight+'px'" stripe style="width: 100%" border>
            <el-table-column prop="kpiDate" label="时间" min-width="80" align="center"></el-table-column>
            <el-table-column prop="kpiLevel" label="绩效等级" min-width="120" align="center"></el-table-column>
            <el-table-column prop="kpiRatio" label="绩效系数" min-width="100" align="center"></el-table-column>
            <el-table-column prop="kpiScores" label="绩效分值" min-width="120" align="center"></el-table-column>
            <el-table-column label="操作" min-width="100" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="showDetailDialog(scope.row)" style="padding: 0;">查看详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-dialog>
    <PersonPerformanceDetail ref="PersonPerformanceDetail"></PersonPerformanceDetail>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import {dateUtil} from '@/utils/dateUtil'
import PersonPerformanceDetail from "./person-performance-detail";
export default {
  data () {
    return {
      tableHeight: '',
      searchData: {
        kpiDate: null,
      },
      departmentList: [],
      userKpiList: [],
      curUser: {
        dingName: '',
        department: '',
      },
      userKpiDialogVisible: false,
    };
  },
  mixins: [dingMixin],
  components: {
    PersonPerformanceDetail
  },
  watch: {
    searchData: {
      handler() {
        this.getDepartmentList()
      },
      deep: true
    }
  },
  computed: {
    hasLimit() {
      return JSON.parse(sessionStorage.getItem('menuId')).includes('1321278912673357826')
    }
  },
  mounted () {
    this.tableHeight = document.documentElement['clientHeight'] - 120
    window.onresize = () => {
      this.tableHeight = document.documentElement['clientHeight'] - 120
    }
    this.searchData.kpiDate = dateUtil.thisMonth();
  },
  methods: {
    async getDepartmentList() {
      this.departmentList = [];
      let curUserDeptPathArr = this.userInfo.deptPath.split(',')
      let curUserDeptId = curUserDeptPathArr.length >= 2 ? curUserDeptPathArr[curUserDeptPathArr.length-2] : ''

      let dept = await this.$httpN.postB('sys/dept/listAll')
      let firstArr = dept.filter(item => {
          item.idsArr = [item.id]
          return item.pid === '1'
      } )
      firstArr.forEach( item => item.hasChildren = true)
      this.departmentList = this.hasLimit ? firstArr : firstArr.filter(dept => dept.id === curUserDeptId)
    },
    async getKpiList(tree, treeNode, resolve) {
      let {list} = await this.$httpN.postB('hr/kpi/list', {
        deptId: tree.id,
        dingId: '',
        kpiDate: this.searchData.kpiDate,
        limit: 999
      })
      resolve(list)
    },
    async getKpiListByUser(row) {
      let {list} = await this.$httpN.postB('hr/kpi/list', {
        dingId: row.dingId,
      })
      this.userKpiList = list
      this.curUser = {
        dingName: row.dingName,
        department: row.department
      },
      this.userKpiDialogVisible = true;
    },
    showDetailDialog(row) {
      this.$nextTick(() => {
        this.$refs.PersonPerformanceDetail.init(row.id, row.dingName, row.department);
      })
    },
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (row.pid) {
        return [1, 6];
        if(columnIndex === 1 || columnIndex === 2 || columnIndex === 3 || columnIndex === 4 || columnIndex === 5) {
          return [0, 0]
        }
      }
    },
  }
}
</script>

<style lang='scss' scoped>
.content-wrapper {
  height: 100vh;
  min-width: 500px;
  .nav {
    display: flex;
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    justify-content: space-between;
    border-bottom: 1px solid #DCDFE6;
    .el-breadcrumb {
      display: flex;
      align-items: center;
      .el-breadcrumb__item {
        float: none;
      }
    }
  }
  .content {
    height: calc(100vh - 50px);
    overflow: auto;
    .search-bar {
      padding: 0 20px;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .main-content {
      padding: 0 20px;
    }
  }
}
.dialog-style {
  /deep/ .el-dialog__header, /deep/ .el-dialog__body {
    padding: 0;
  }
  .title {
    text-align: center;
    width: 100%;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #DCDFE6;
    position: relative;
    .back {
      position: absolute;
      left: 20px;
      top: 7px;
      cursor: pointer;
    }
  }
  .content {
    padding: 0 20px;
    .info {
      display: flex;
      line-height: 50px;
      .info-item {
        margin-right: 10px;
        display: flex;
        align-items: center;
        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>